class SetOnceMappingMixin:
    __slots__ = ()

    def __setitem__(self, key, value):
        if key in self:
            raise KeyError(str(key) + ' already set')
        return super().__setitem__(key, value)


class SetOnceDict(SetOnceMappingMixin, dict):
    pass


def main():
    dict1 = SetOnceDict()
    try:
        dict1['username'] = 'jackfrued'
        dict1['username'] = 'hellokitty'
        dict1['username'] = 'wangdachui'
    except KeyError:
        pass
    print(dict1)


if __name__ == '__main__':
    main()
